/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import dao.Dao;
import entidades.Album;
import entidades.Musica;
import entidades.Produtora;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;

/**
 *
 * @author aluno
 */
@WebServlet(name = "ServletAlbum", urlPatterns = {"/ServletAlbum"})
public class ServletAlbum extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ServletAlbum</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet ServletAlbum at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
             */
        } finally {
            out.close();
        }
    }

    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String op = request.getParameter("op");
        String nome = (String) request.getParameter("nome");
        String data = (String) request.getParameter("data");

        Date date = dao.Util.stringToDate(data);
        String prod = request.getParameter("produtora");
        String[] valores = request.getParameterValues("musicas");

        int ind = Integer.parseInt(prod);

        Album a = new Album();
        Dao<Produtora> dao = new Dao<Produtora>(Produtora.class);
        Dao<Musica> dao2 = new Dao<Musica>(Musica.class);
        a.setNome(nome);
        a.setAno(date);
        a.setProdutora(dao.get(ind));
        ArrayList<Integer> array = new ArrayList();
        if (op.equals("cadastrar")) {
            for (int i = 0; valores.length > i;) {
                array.add(Integer.parseInt(valores[i]));
                i++;
            }
            ArrayList<Musica> musics = new ArrayList<Musica>();
            for (int i = 0; array.size() > i;) {
                musics.add(dao2.get(array.get(i)));
                i++;
            }
            a.setMusicas(musics);
            Dao<Album> daoa = new Dao<Album>(Album.class);
            daoa.insert(a);


        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
